#ifndef SESSIONFRIENDAREA_H
#define SESSIONFRIENDAREA_H

#include <QWidget>
#include <QScrollArea>
#include <QIcon>
#include <QString>
#include <QEvent>
#include <QPushButton>
#include <QObject>
#include <QMouseEvent>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLabel>

/* --------------------  Item 类型 -------------------- */
enum ItemType{
    SessionItemType,
    FriendItemType,
    ApplyItemType
};


/* -------------------- 滚动区域的实现 -------------------- */
class SessionFriendArea : public QScrollArea
{
    Q_OBJECT
public:
    explicit SessionFriendArea(QWidget *parent = nullptr);

    // 清空所有Item
    void clear();
    // 添加 Item -- text 表示 签名/最后一条消息
    void addItem(ItemType itemType, const QString&id, const QIcon& avatar, const QString& name, const QString& text = "", const QString& time = "");
    // 点击指定下标元素
    void clickItem(int index);

    // // 删除指定下标 Item
    // void delItem(int index);
    // // 隐藏指定下标 Item
    // void hideItem(int index);

protected:
    bool eventFilter(QObject *obj, QEvent *event) override;

private:
    // 后续往 container 内部的 layout 添加元素, 就能触发 QScrollArea 效果
    QWidget* container;
    QString vScrollBarStyle;


    void setupScrollBars();
    void showVerticalScrollBar();
    void hideVerticalScrollBar();

signals:
};

/* -------------------- 滚动区域中 Item 的实现 -------------------- */
class SessionFriendItem: public QWidget
{
    Q_OBJECT
public:
    SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text = "", const QString& time = "");

    virtual void active(){} // 元素被点击时的反应
    void select();  // 选中这项该做的事情

protected:
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void enterEvent(QEnterEvent* event)override;
    void leaveEvent(QEvent* event)override;

    // 把关键控件暴露为 protected
    QLabel* nameLabel = nullptr;
    QLabel* messageLabel = nullptr;
    QLabel* timeLabel = nullptr;
    QPushButton* avatarBtn = nullptr;

    // 布局也暴露
    QGridLayout* layout = nullptr;

    bool selected = false; // 表示 Item 是否选中

private:
    QWidget* owner;
};


/* -------------------- 会话 Item 的实现 -------------------- */
class SessionItem: public SessionFriendItem
{
    Q_OBJECT
public:
    SessionItem(QWidget* owner, const QString& chatSessionId, const QIcon& avatar, const QString& name, const QString& lastMessage, const QString& time = "");
    void active() override;
    void updateLastMessage(const QString &chatSessionId);

private:
    QString chatSessionId;  // 当前会话 id
    QString text;           // 最后一条消息的文本预览
};


/* -------------------- 好友 Item 的实现 -------------------- */
class FriendItem: public SessionFriendItem
{
    Q_OBJECT
public:
    FriendItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name, const QString &description);
    void active() override;

private:
    QString userId; // 好友的用户 id
};


/* -------------------- 好友申请 Item 的实现 -------------------- */
class ApplyItem: public SessionFriendItem
{
    Q_OBJECT
public:
    ApplyItem(QWidget* owner, const QString& userId, const QIcon& avatar, const QString& name);
    void active() override;

    void acceptFriendApply();
    void rejectFriendApply();

private:
    QString userId; // 好友的用户 id
};



#endif // SESSIONFRIENDAREA_H















